#include "SettingHelper.h"

const double fDefaultHorizontal(1.d);
const double fDefaultVertical(1.d);
const double fDefaultZoomRatio(1.d);

QString SettingHelper::strINIPath = QFileInfo(QDir::current(), strINIFilename).absoluteFilePath();

void SettingHelper::initSetting()
{
	if(!QFile::exists(SettingHelper::strINIPath))
	{
		SettingHelper::saveCalibration(fDefaultHorizontal, fDefaultVertical);
		SettingHelper::saveZoomRatio(fDefaultZoomRatio);
		SettingHelper::saveFullScreen(true);
		SettingHelper::saveDiagnosis(QDir::currentPath(), "bmp");
	}
}

void SettingHelper::saveCalibration(double h, double v)
{
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	setting.beginGroup(strCfgCalibration);
	setting.setValue(strCfgHorizontal, h);
	setting.setValue(strCfgVertical, v);
	setting.endGroup();
}

void SettingHelper::loadCalibration(double &h, double &v)
{
	SettingHelper::initSetting();
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	setting.beginGroup(strCfgCalibration);
	h = setting.value(strCfgHorizontal).toDouble();
	v = setting.value(strCfgVertical).toDouble();
	setting.endGroup();
}

void SettingHelper::saveZoomRatio(double z)
{
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	setting.beginGroup(strCfgCalibration);
	setting.setValue(strCfgZoomRatio, z);
	setting.endGroup();
}

void SettingHelper::loadZoomRatio(double &z)
{
	SettingHelper::initSetting();
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	setting.beginGroup(strCfgCalibration);
	z = setting.value(strCfgZoomRatio).toDouble();
	setting.endGroup();
}

void SettingHelper::saveFullScreen(bool b)
{
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	setting.setValue(strCfgFullScreen, b);
}

void SettingHelper::loadFullScreen(bool &b)
{
	SettingHelper::initSetting();
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	b = setting.value(strCfgFullScreen).toBool();
}

void SettingHelper::saveDiagnosis(QString exePath, QString saveExt)
{
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	setting.beginGroup(strCfgDiagnosis);
	setting.setValue(strCfgDiagnosisExePath, exePath);
	setting.setValue(strCfgSaveExt, saveExt);
	setting.endGroup();
}

void SettingHelper::loadDiagnosis(QString *exePath, QString *saveExt)
{
	SettingHelper::initSetting();
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	exePath->clear();
	saveExt->clear();
	setting.beginGroup(strCfgDiagnosis);
	exePath->append(setting.value(strCfgDiagnosisExePath).toString());
	saveExt->append(setting.value(strCfgSaveExt).toString());
	setting.endGroup();
}

void SettingHelper::saveConsole(QString exePath)
{
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	setting.setValue(strCfgConsoleExePath, exePath);
}

void SettingHelper::loadConsole(QString *exePath)
{
	SettingHelper::initSetting();
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	exePath->clear();
	exePath->append(setting.value(strCfgConsoleExePath).toString());
}

void SettingHelper::saveImagePath(QString path)
{
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	setting.setValue(strCfgImageSavePath, path);
}

void SettingHelper::loadImagePath(QString *path)
{
	SettingHelper::initSetting();
	QSettings setting(SettingHelper::strINIPath, QSettings::IniFormat);

	path->clear();
	path->append(setting.value(strCfgImageSavePath).toString());
}
